<?php
/**部门管理
 * Created by leiying
 * Date: 2018-06-04
 * Time: 14:05:49
 */

namespace Admin\Controller;


class DepartmentController extends AdminController
{
    /**列表
     * @author leiying
     */
    public function DepartmentList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_DepartmentList',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_DepartmentList',null);
            }
            $get = $search_data = session('searchdata_DepartmentList');
        }
        //搜索条件：部门名称
        if(isset($get['name']) && $get['name'] != ''){
            $search_data['name'] = trim($get['name']);
            $where['name'] = array('like','%'.$search_data['name'].'%');
        }
        $where["status"] = 1;
        $count = M('department')->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString =$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('department')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();
        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);
        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }


    /**批量操作
     * @param null $method
     * @author leiying
     */
    public function changeStatus($method=null){
        $id = I('post.id');
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'deletegroup':
                $res = M("department")->where(array("id"=>array('in',$id)))->delete();
                if($res){
                    $this->success("删除成功");
                }else{
                    $this->error("删除失败");
                }
                break;
            default:
                $this->error($method.'参数非法');
        }
    }

    /**添加
     * @author leiying
     */
    public function DepartmentAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            /*处理参数(请根据业务逻辑适当修改)*/
            //部门名称
            $name = trim($post['name']);
            if(empty($name)){
                $this->error("部门名称不能为空");
            }
            $data = array(
                'name'=>$name,
                'status' => 1,
                'add_time'=>time(),
            );
            $res = M('department')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }

    /**编辑
     * @author leiying
     */
    public function DepartmentEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            /*处理参数(请根据业务逻辑适当修改)*/
            //部门名称
            $name = trim($post['name']);
            if(empty($name)){
                $this->error("部门名称不能为空");
            }
            $data = array(
                'name'=>$name,
            );
            $res = M('department')->where(array("id"=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('department')->where(array('id'=>$id))->find();
            $this->assign('res',$res);
            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

}